<?php
global $_W, $_GPC;
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if ('post' == $op) {
	$id = intval($_GPC['id']);
	if (!empty($id)) {
		$row = pdo_fetch("SELECT * FROM " . tablename($this->tabletop) . " WHERE id = :id", array(':id' => $id));
		if (empty($row)) {
			message('数据不存在！', '', 'error');
		}
	}
	if (checksubmit('submit')) {
		$title = trim($_GPC['title']);
		if (empty($title)) {
			message('标题不能为空!');
		}
		$data = array('uniacid' => $_W['uniacid'], 'title' => $title, 'fxdec' => $_GPC['fxdec'], 'thumb' => $_GPC['thumb'], 'jumpurl' => $_GPC['jumpurl'], 'status' => $_GPC['status'], 'displayorder' => $_GPC['displayorder'],);
		if (empty($id)) {
			pdo_insert($this->modulename . '_top', $data);
			message('添加成功！', $this->createWebUrl('listtop', array('op' => 'display')), 'success');
		} else {
			pdo_update($this->modulename . '_top', $data, array('id' => $id));
			message('更新成功！', $this->createWebUrl('listtop', array('op' => 'display')), 'success');
		}
	}
} else if ('del' == $op) {
	$id = intval($_GPC['id']);
	$row = pdo_fetch("SELECT id FROM " . tablename($this->tabletop) . " WHERE id = :id", array(':id' => $id));
	if (empty($row)) {
		message('特推不存在！');
	}
	pdo_delete($this->modulename . '_top', array('id' => $id));
	message('删除成功！', referer(), 'success');
} else if ('delall' == $op) {
	$ids = $_GPC['ids'];
	foreach ($ids as $id) {
		pdo_delete($this->modulename . '_top', array('id' => $id));
	}
	message('删除' . $id . '成功！', referer(), 'success');
} else if ('display' == $op) {
	if (checksubmit('submit')) {
		$displayorders = $_GPC['displayorder'];
		foreach ($displayorders as $id => $displayorder) {
			pdo_update($this->modulename . '_top', array('displayorder' => $displayorder), array('id' => $id));
		}
		message('批量更改排序成功', referer(), 'success');
	}
	$adsall = pdo_fetchall("SELECT * FROM " . tablename($this->tabletop) . " WHERE uniacid = '{$_W['uniacid']}'");
	$condition1 = "";
	if (!empty($_GPC['keyword'])) {
		$condition1 .= " AND CONCAT(`name`,`id`) LIKE '%{$_GPC['keyword']}%'";
	}
	$pindex = max(1, intval($_GPC['page']));
	$psize = 20;
	$list = pdo_fetchall("SELECT * FROM " . tablename($this->tabletop) . " WHERE uniacid = '{$_W['uniacid']}' $condition1 order by displayorder desc  LIMIT " . ($pindex - 1) * $psize . ',' . $psize);
	$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename($this->tabletop) . " WHERE uniacid = '{$_W['uniacid']}' $condition1 $condition2");
	$pager = pagination($total, $pindex, $psize);
} else if ('status' == $op) {
	$id = $_GPC['id'];
	pdo_query('update ' . tablename($this->tabletop) . " set status = !status where id='{$id}'");
	die('1');
}
include $this->template('listtop');